/*
 * @Author: your name
 * @Date: 2021-07-27 10:23:53
 * @LastEditTime: 2021-07-29 15:02:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \base-framework-web\src\utils\tools.ts
 */
import { message } from 'antd';
import moment from 'moment';
/**
 *
 * @param {Blob} blobData //blob数据
 * @param {String} fileName //文件名
 */
export const exportExcel = (blobData: Blob, fileName: string, suffix = '.CSV') => {
  let blob = new Blob([blobData], {
    type:
      'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  });
  let downloadElement = document.createElement('a');
  let nowTime = moment(Date.now()).format('YYYY-MM-DD HH_ss_mm');
  let lastFileName = `${fileName}${nowTime}${suffix}`;
  let href = window.URL.createObjectURL(blob);
  document.body.appendChild(downloadElement);
  downloadElement.href = href;
  //兼容IE
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    downloadElement.addEventListener('click', () => {
      window.navigator.msSaveOrOpenBlob(blob, lastFileName);
    });
  } else {
    downloadElement.download = lastFileName;
  }
  document.body.appendChild(downloadElement);
  downloadElement.click();
  document.body.removeChild(downloadElement);
  message.loading('正在导出');
  window.URL.revokeObjectURL(href); //释放掉blob对象
};

/**
 * //转换为formData对象
 * @param {Object} msg 参数
 */
export const fomatFormData = (msg: any) => {
  let params = new URLSearchParams();
  for (let key in msg) {
    params.append(key, msg[key]);
  }
  return params;
};

/**
 * 
 * @param {String[]} keysArr 键数组
 * @param {Any[]} dataSource 目标原数据数组
 * @param {String}key 指定键
 */
export const filterAssignArr = (keysArr: string[], dataSource: any[], key: string) => {
  let arr: any[] = [];
  keysArr.forEach((item) => {
    dataSource.forEach((ele) => {
      if (item === ele[key]) {
        arr.push(ele);
      }
    });
  });
  return arr;
}

export const formatTreeData = (data: any) => {
  if (Array.isArray(data) && data.length > 0) {
    data.forEach((ele) => {
      ele.title = ele.name;
      ele.key = ele.id;
      if (ele.children && ele.children.length > 0) {
        formatTreeData(ele.children);
      }
    });
  }
  return data;
};

export const formatStatusMap = (data: any[]) => {
  if (Array.isArray(data) && data.length > 0) {
    let obj = {};
    data.forEach(ele => {
      obj[+ele.dictValue] = ele.dictLabel;
    })
    return obj;
  }
}

//复制
export const copyFun = (copyText) => {
  let tempinputDom = document.createElement("input");
  document.body.appendChild(tempinputDom);
  tempinputDom.value = copyText;
  tempinputDom.select();
  document.execCommand("copy");
  document.body.removeChild(tempinputDom);
}

export const setStorageData = (key, val) => {
  let formatData = JSON.stringify(val);
  sessionStorage && sessionStorage.setItem(key, formatData);
}

export const getStorageData = (key: string, isObject: boolean = true) => {
  let data;
  if (sessionStorage.getItem(key)) {
    data = isObject ? JSON.parse(sessionStorage.getItem(key) as any) : sessionStorage.getItem(key);
  } else {
    data = isObject ? {} : '';
  }
  return data
}